/*
 * @Author: zwc 6537397+uni-yunApp@user.noreply.gitee.com
 * @Date: 2023-11-13 15:25:52
 * @LastEditors: zwc 6537397+uni-yunApp@user.noreply.gitee.com
 * @LastEditTime: 2023-12-25 17:05:55
 * @FilePath: \covue-main\lib\core\actions.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */


const ora = require("ora");
// console
const chalk = require('chalk')
const figlet =  require('figlet')
// 创建应用和页面的方法
const { formatData, getGitUrl,getPage,spinnerMethod,getGithubProject} = require('./utils')
// terminal commands
const terminalCommands = require('../commands/terminal')

// inquirer,命令行交互
const inquirer = require('inquirer');
const { name } = require('commander');

const createProject = async (project) => {
  getGithubProject(project,'MrZhengwencai')
}
// 用户自定义github账号开源仓库项目拉取
function getGithubUserProjects(userName){
  getGithubProject(null,userName)
}
function firstUpperCase(str) {
  return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
}
// 创建页面
const createPage = async (name,pageType,needFolder) => {

  const typeList = ['v2', 'v3']
  // 模板类型
  const type = typeList.includes(pageType)
  // 是否需要文件夹
  const isNeed = needFolder === 'y' ? true : false
  const pageTypeEnums = {
    v2: 'vue2PageTemplate',
    v3: 'vue3PageTemplate',
  }
  const props = {
    name,
    lowerName: name,
    firstUpperName: name,
    isNeed: isNeed,
  }
  // 判断名称是否存在
  if(!name){
    console.log('\r\n' + '请输入页面名称')
    return
  }
  if (name && pageType && type) {
    try {
      getPage(props,pageTypeEnums[pageType])
    
    } catch (error) {
      console.log(error)
    }
  } else {
    // 如果有输入模板类型指令则需要校验指令是否正确
    if (pageType) {
      if (!type) {
        console.log(
          '\r\n' +
            '-v2表示vue2模板、-v3表示vue3模板,' +
            '暂无' +
            pageType +
            '模板类型'
        )
        return
      }
      getPage(props,pageTypeEnums[pageType])
    } else {
     
      // 无文件类型的话 直接走原先的选择交互
      const answer = await inquirer.prompt([
        {
          type: 'list',
          name: 'pageTemplate',
          message: '请选择一个页面模板:',
          choices: ['vue2PageTemplate', 'vue3PageTemplate'],
        },
      ])
      // 添加一个选择，是否需要文件夹
      const needfolder = await inquirer.prompt([
        {
          type: 'list',
          name: 'isNeedfolder',
          message: '是否需要生成同名文件夹:',
          choices: ['yes', 'no'],
        },
      ])
   
      if(needfolder.isNeedfolder==='yes'){
        props.isNeed = true
      }else{
        props.isNeed = false
      }
      getPage(props,answer.pageTemplate)
    }

  }
}

module.exports = {
  createProject,
  createPage,
  getGithubUserProjects
};